package org.grow.webchat.chat;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.filter.RequestContextFilter;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

import java.util.List;

@Configuration
@EnableWebSocketMessageBroker
public class ChatBrokerConfig implements WebSocketMessageBrokerConfigurer {
    public static final String BrokerTitle="/title";
    public static final String BrokerNotice="/notice";
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/endpoint")
                .setAllowedOrigins("http://localhost:8080",
                        "http://127.0.0.1:8080",
                        "http://192.168.0.7:8080")
                .withSockJS();
    }

//    @Override
//    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
//        return false;
//    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker(BrokerTitle,BrokerNotice);
        registry.setApplicationDestinationPrefixes("/wsapp");
        registry.setUserDestinationPrefix("/user");
//      意味着客户端　发送消息　/wsapp/@MessageMapping
//        客户端监听信息　/user/topic/xxx|yyy
//        但是尝试了一下发现客户端监听的是SendTo注解的值　topic/xxx
//        或许是ｔｅｍｐｌａｔｅ发送？？？
//        ：todo
//        /*
//        setUserDestinationPrefix 设置点对点时,
//        destination 的前缀, 如客户端订阅 /user/{userId}/getPoint,
//        服务端发送消息时, 调用 messagingTemplate.convertAndSendToUser
//        (userId, "/getPoint", msg)

    }
}
